package task

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"iot-base/auth/api/logic"
	types "iot-base/auth/types"
)

func CmdAddTask(c *gin.Context) {
	resp := types.NewResult(c)
	var req types.ReqAddTask
	err := c.ShouldBind(&req)
	if err != nil {
		resp.ErrorCode(types.ParamsError, "")
		return
	}
	// 启用 禁用
	if req.Switch == "" {
		req.Switch = "enable"
	}
	rules := make([]types.CaliParams, 0, 1)
	rules = append(rules, types.CaliParams{
		GasType:   types.IsZero(req.TaskType),
		Number:    float64(req.Number),
		Ext:       req.Ext,
		Unit:      types.GetUnit(req.ModelId),
		ValueTime: []int{},
		Flow:      req.Flow,
	})
	b, _ := json.Marshal(rules)
	_, err = logic.NewTaskLogic().AddTask(req, b)
	if err != nil {
		resp.ErrorCode(types.Fail, "添加任务失败.")
		return
	}
	resp.Success(gin.H{})
}
